/*
 * DateChooserControl.java
 *
 * Created on 25 marzec 2007, 18:42
 */

package datechooser;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author  Przemek
 */
public class DateChooserControl extends javax.swing.JPanel
{
    
    private Date date;
    
    static private SimpleDateFormat dateFormatter = new SimpleDateFormat( "dd-MM-yyyy");
    
    /** Creates new form DateChooserControl */
    public DateChooserControl()
    {
        this( null );
    }
    
    public DateChooserControl( Date date)
    {        
        initComponents();
        this.setDate( date );
        this.initializeListeners();              
    }
    
    protected void initializeListeners()
    {
        jTextFieldDate.addFocusListener( new FocusListener()
        {
            public void focusGained(FocusEvent e)
            {
            }
            public void focusLost(FocusEvent e)
            {
                setDate( getDateFromTextBox() );
            }
        });
        
        jButtonOpenWindow.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {   
                DateChooserDialog dlg = new DateChooserDialog( true );
                
                
                if( date != null)
                    dlg.setDate( date );
                else
                {
                    //today = data z godziny 00:00 dnia dzisiejszego
                    Calendar today = Calendar.getInstance();
                    today.set( Calendar.MILLISECOND, 0 );
                    today.set( Calendar.SECOND, 0 );
                    today.set( Calendar.MINUTE, 0 );
                    today.set( Calendar.HOUR_OF_DAY, 0 );
                    
                    dlg.setDate( today.getTime() );
                }
                
                dlg.setLocation( getX(), getY() + getHeight());
                dlg.setSize( 400, 60 );
                
                dlg.setVisible( true );
                
                setDate( dlg.getDate() );
                dlg.dispose();
            }
        });
    }
    
    private Date getDateFromTextBox()
    {
        Date result;
        try
        {
            result = dateFormatter.parse( jTextFieldDate.getText() );
        }
        catch( ParseException e )
        {
            java.awt.Toolkit.getDefaultToolkit().beep();
            if( date != null )
                jTextFieldDate.setText( dateFormatter.format( date ) );
            else
                jTextFieldDate.setText( "" );
                    
            result = date;
        }
        return result;
    }
    
    public void setEnabled( boolean enabled )
    {
        super.setEnabled( enabled );
        
        jButtonOpenWindow.setEnabled( enabled );
        jTextFieldDate.setEnabled( enabled );
    }
            
    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {       
        this.date = date;
        if (date != null)
            jTextFieldDate.setText( dateFormatter.format( date ) );
        else
            jTextFieldDate.setText( "" );
            
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        jTextFieldDate = new javax.swing.JTextField();
        jButtonOpenWindow = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jTextFieldDate, gridBagConstraints);

        jButtonOpenWindow.setFont(new java.awt.Font("Tahoma", 1, 14));
        jButtonOpenWindow.setText("v");
        jButtonOpenWindow.setMargin(new java.awt.Insets(2, 8, 2, 8));
        jButtonOpenWindow.setPreferredSize(new java.awt.Dimension(30, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 0);
        add(jButtonOpenWindow, gridBagConstraints);

    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOpenWindow;
    private javax.swing.JTextField jTextFieldDate;
    // End of variables declaration//GEN-END:variables
    
}
